/**
 * @file init.c
 * @brief initalize the system, so the game could run
 */

/* Allegro */
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
/* Standard */
#include <stdio.h>
#include <time.h>
/* Project */
#include <blasteroids.h>

bool *keys = NULL;
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_EVENT_QUEUE *queue = NULL;
ALLEGRO_EVENT *event = NULL;

void blasteroids_init_control(void) {
  if (!al_init()) {
    perror("Couldn't initialize the allegro system!");
    exit(-1);
  }
  if (!al_install_keyboard()) {
    perror("Couldn't initialize the keyboard!");
    exit(-1);
  }
  if (!al_init_primitives_addon()) {
    perror("Couldn't initialize the primitives!");
    exit(-1);
  }
  return;
}

void blasteroids_init_display(void) {
  display = al_create_display(WIDTH, HEIGHT);
  if (!display) {
    perror("Couldn't create display!\n");
    exit(-1);
  }
}

void blasteroids_init_timer(void) {
  timer = al_create_timer(1.0 / FPS);
  if (!timer) {
    perror("Couldn't create timer\n");
    exit(-1);
  }
}

void blasteroids_init_events() {
  event = calloc(1, sizeof(ALLEGRO_EVENT));
  queue = al_create_event_queue();
  if (!queue) {
    perror("Couldn't create event queue!\n");
    exit(-1);
  }
  al_register_event_source(queue, al_get_display_event_source(display));
  al_register_event_source(queue, al_get_timer_event_source(timer));
  al_register_event_source(queue, al_get_keyboard_event_source());
  al_start_timer(timer);
  if (!al_get_timer_started(timer)) {
    perror("Couldn't start the timer!\n");
    exit(-1);
  }
}
